package com.qhg.saas_server.controller;

import com.github.pagehelper.PageInfo;
import com.qhg.saas_server.empty.Result;
import com.qhg.saas_server.pojo.Store;
import com.qhg.saas_server.pojo.dto.StoreInfoDto;
import com.qhg.saas_server.service.impl.StoreServiceImpl;
import com.qhg.saas_server.utils.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/Store")
public class StoreController {

    @Autowired
    StoreServiceImpl storeService;
    @Autowired
    HttpServletResponse response;

    /*模糊查询门店的信息列表*/
    @PostMapping("/SelectAll")
    public Result SelectAll (Integer pageNum,Integer pageSize,String name,String comploPhone,String BeforeTime,String AfterTime){
        PageInfo pageInfo = storeService.SelectAll(pageNum, pageSize, name, comploPhone, BeforeTime, AfterTime);
        return Result.success(pageInfo);
    }
    /*通过ID搜索门店*/
    @GetMapping("/SelectById")
    public Result SelectById(Integer storeId){
        StoreInfoDto storeInfoDto = storeService.SelectById(storeId);
        return Result.success(storeInfoDto);
    }
    /*插入门店信息*/
    @PostMapping("/InsertStore")
    public Result InsertStore(Store store){
        storeService.InsertStore(store);
        return Result.buildSuccess();
    }
    /*修改门店信息*/
    @PostMapping("/UpdateStore")
    public Result UpdateStore(Store store){
        storeService.UpdateStore(store);
        return Result.buildSuccess();
    }
    /*通过ID删除门店信息*/
    @GetMapping("/DeleteStoreById")
    public Result DeleteStoreById(Integer storeId){
        storeService.DeleteStoreById(storeId);
        return Result.buildSuccess();
    }
    /*门店信息导出为Excel文件*/
    @PostMapping("/ExportExcel")
    public Result ExportExcel(Integer pageNum,Integer pageSize,String name,String comploPhone,String BeforeTime,String AfterTime){
        PageInfo<StoreInfoDto> pageInfo = storeService.SelectAll(pageNum, pageSize, name, comploPhone, BeforeTime, AfterTime);
        List<StoreInfoDto> list = pageInfo.getList();
        ExcelUtil.exportExcel(list,"门店信息表","sheet1",StoreInfoDto.class,"门店信息表.xls",response);
        return Result.buildSuccess();
    }
}
